package com.aizhixin.lab.task.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.course.entity.Course;
import com.aizhixin.lab.course.course.service.CourseService;
import com.aizhixin.lab.task.domain.QueryTaskDomain;
import com.aizhixin.lab.task.domain.QueryTaskTemplateDomain;
import com.aizhixin.lab.task.domain.TaskDomain;
import com.aizhixin.lab.task.domain.TaskTemplateDomain;
import com.aizhixin.lab.task.entity.Task;
import com.aizhixin.lab.task.entity.TaskStuInfor;
import com.aizhixin.lab.task.service.TaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/task")
@Api(value = "实验报告相关API")
public class TaskController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/getcourselist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "教师我的备课列表", response = Void.class, notes = "教师我的备课列表<br>@author zhengning")
    public ResponseEntity<?> getCourseList(
            @RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        List<Course> list = courseService.findCourseByCreatedByAndIsPublishAndIsArchive(account.getId(), false, false);
        Map<String, Object> result = new HashMap<>();
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.DATA, list);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "创建/修改课程任务名称", response = Void.class, notes = "保存课程任务名称<br>@author zhengning")
    public ResponseEntity<?> save(
            @RequestHeader("Authorization") String token,
            @ApiParam(value = "id存在为修改,courseId:课程id(必填),课程章节Id:courseChapterId,课程小节Id:courseMinutiaId,taskName:任务名称(必填)", required = false) @RequestBody TaskDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = new HashMap();
        if (StringUtils.isEmpty(domain.getTaskName())) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "任务名称不能为空！");
            return new ResponseEntity(result, HttpStatus.OK);
        }
//		if (null !=domain.getTemplateId() && domain.getTemplateId().longValue() > 0L) {
//		}else {getTaskRank
//			result.put(ApiReturnConstants.SUCCESS, false);
//			result.put(ApiReturnConstants.ERROR, "课程模板id不能为空！");
//			return new ResponseEntity(result, HttpStatus.OK);
//		}
        if (StringUtils.isEmpty(domain.getCourseId())) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "课程id不能为空！");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        result = taskService.save(domain, account.getId());
        return new ResponseEntity(result, HttpStatus.OK);
    }


    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除任务", response = Void.class, notes = "删除任务<br>@author zhengning")
    public ResponseEntity<?> delete(
            @RequestHeader("Authorization") String token,
            @ApiParam(value = "id 任务id") @RequestParam(value = "id", required = true) String id) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = new HashMap();
        if (StringUtils.isEmpty(id)) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "任务id不能为空！");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        Task task = taskService.findById(id);
        if (null != task) {
            task.setDeleteFlag(DataValidity.INVALID.getState());
            taskService.save(task);
            result.put(ApiReturnConstants.SUCCESS, true);
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "id对应的任务不存在！");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findTask", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查看单个任务详情", response = Void.class, notes = "查看单个任务详情<br>@author lwq")
    public ResponseEntity<?> findPag(
            @RequestHeader("Authorization") String token,
            @ApiParam(value = "taskId:任务id") @RequestParam(value = "id") String taskId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        Task task = taskService.findById(taskId);
        return new ResponseEntity(task, HttpStatus.OK);
    }

    @RequestMapping(value = "/findpage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "分页查询课程模板下任务列表", response = Void.class, notes = "分页查询课程模板下任务列表<br>@author zhengning")
    public ResponseEntity<?> findPage(
            @RequestHeader("Authorization") String token,
            @ApiParam(value = "templateId:课程模板id", required = false) @RequestBody QueryTaskDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = new HashMap();
        if (null != domain.getTemplateId() && domain.getTemplateId().longValue() > 0L) {
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "课程模板id不能为空！");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        result = taskService.findTaskPage(domain);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findTaskTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "分页查询课程下任务列表", response = Void.class, notes = "分页查询课程模板下任务列表<br>@author lwq")
    public ResponseEntity<?> findTaskTemplate(
            @RequestHeader("Authorization") String token,
            @ApiParam(value = "templateId:模板id", required = false) @RequestBody QueryTaskTemplateDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = new HashMap();
        if (StringUtils.isEmpty(domain.getTemplateId() == null || domain.getTemplateId() == 0L)) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "模板id不能为空！");
            return new ResponseEntity(result, HttpStatus.OK);
        }

        List<TaskTemplateDomain> taskTemplateList = taskService.findTaskTemplateList(domain, account);
        result.put(ApiReturnConstants.DATA, taskTemplateList);
        return new ResponseEntity(result, HttpStatus.OK);
    }

}
